package com.jk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.dao.SysMenuMapper;
import com.jk.dao.TemUserMapper;
import com.jk.dto.SysMenu;
import com.jk.dto.TemUser;
import com.jk.pojo.PagedResponse;
import com.jk.service.TemUserService;


import com.jk.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author yzf
 * @version 1.0
 * @ClassName TemUserServiceImpl
 * @program: yzf-test
 * @description: TODO
 * @date 2025/2/27 19:49
 */
@Service
public class TemUserServiceImpl implements TemUserService {
    @Autowired
    private TemUserMapper temUserMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<TemUser> findUserList(TemUser temUser) {

        return temUserMapper.selectList(null);
    }

    @Override
    //事务注解   事务的回滚  要么全部成功 要么全部失败
    @Transactional(rollbackFor = Exception.class)
    public void addUser(TemUser temUser) {
//        IdWorker idWorker = new IdWorker();
//        temUser.setId((int) idWorker.nextId());
        temUserMapper.insert(temUser);
    }

    @Override
    public PagedResponse<TemUser> findUserListByPage(TemUser temUser, Integer pageNum, Integer pageSize) {
        Page<TemUser> page = new Page<>(pageNum, pageSize);
        Page<TemUser> temUserIPage = temUserMapper.selectPage(page, null);
        return PaginationUtil.getPaginationResponse(temUserIPage.getRecords(), pageNum, pageSize, temUserIPage.getTotal());
    }

    @Override
    public TemUser findTemUserByUserName(String username) {
        QueryWrapper<TemUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        TemUser temUser = temUserMapper.selectOne(queryWrapper);
        if (Objects.isNull(temUser)) {
            throw new RuntimeException("用户信息不存在！");
        }
        //查询当前用户的菜单权限
        List<SysMenu> menus = new ArrayList<>();
        //通过当前用户id获得当前的登录人的菜单集合
        List<SysMenu> menuList = sysMenuMapper.findMenuListByUserId(temUser, 0L);
        menuList.stream().forEach(item -> {
            //判断当前节点下是否有子节点
            if (isExsistsChildrenNodes(temUser, item)) {
                //查询该节点的字节信息
                List<SysMenu> menuChildrenNodes = findMenuChildrenNodes(temUser, item);
                item.setChildren(menuChildrenNodes);
            }
            menus.add(item);
        });
        temUser.setMenus(menus);
        return temUser;
    }
    private List<SysMenu> findMenuChildrenNodes(TemUser tbUser, SysMenu Menu) {
        List<SysMenu> menus = new ArrayList<>();
        List<SysMenu> menuListByUserId = sysMenuMapper.findMenuListByUserId(tbUser, Menu.getMenuId());
        menuListByUserId.stream().forEach(item -> {
            //判断当前节点下是否有子节点
            if (isExsistsChildrenNodes(tbUser, item)) {
                //查询该节点的字节信息
                List<SysMenu> menuChildrenNodes = findMenuChildrenNodes(tbUser, item);
                item.setChildren(menuChildrenNodes);
            }
            menus.add(item);
        });

        return menus;
    }
    private boolean isExsistsChildrenNodes(TemUser tbUser, SysMenu item) {
        boolean flag = true;
        List<SysMenu> menuListByUserId = sysMenuMapper.findMenuListByUserId(tbUser, item.getMenuId());
        if (CollectionUtils.isEmpty(menuListByUserId)) {
            flag = false;
        }
        return flag;
    }



    @Override
    public List<String> selectPermsByUserId(Integer id) {
        return sysMenuMapper.selectPermsByUserId(id);
    }


}
